/**
 * 1523 在区间范围内统计奇数数目
 *
 * @author suncp
 * @date 2021/1/6 9:37
 */
public class CountOdds {

    public static void main(String[] args) {
        int low = 7;
        int high = 1;
        int i = countOdds2(low, high);
        System.out.println(i);
    }


    /**
     * 660ms|35.1MB
     *
     * @param low
     * @param high
     * @return
     */
    public static int countOdds(int low, int high) {
        int sum = 0;
        if (high < low) {
            return 0;
        }
        // 将low增为奇数
        if (low % 2 == 0) {
            low++;
        }
        while (low <= high) {
            sum++;
            low += 2;
        }
        return sum;
    }

    /**
     * 0ms|38.4ms
     *
     * @param low
     * @param high
     * @return
     */
    public static int countOdds2(int low, int high) {
        if (high < low) {
            return 0;
        }
        if (low % 2 == 0) low += 1;
        if (high % 2 == 0) high -= 1;
        return (high - low) / 2 + 1;
    }

    /**
     * 0ms|35.1MB
     *
     * @param low
     * @param high
     * @return
     */
    public static int countOdds3(int low, int high) {
        if (low % 2 == 0) {
            low++;
        }
        return ((high - low) >> 1) + 1;
    }
}
